package com.task.core.service.impl;


import com.task.common.enums.DataFileTypeEnum;
import com.task.common.enums.DuplicateConditionEnum;
import com.task.common.utils.SystemFunctionUtil;
import com.task.core.domain.DataOperationInfo;
import com.task.core.domain.DatasourceInfo;
import com.task.core.domain.MissionInfo;
import com.task.core.domain.info.RelationMissionDataInfo;
import com.task.core.domain.info.SourceConfig;
import com.task.core.mapper.DataOperationInfoMapper;
import com.task.core.mapper.DatasourceMapper;
import com.task.core.service.DataOperationInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class DataOperationInfoServiceImpl implements DataOperationInfoService {

    @Resource
    private DataOperationInfoMapper dataOperationInfoMapper;

    @Resource
    private DatasourceMapper datasourceMapper;


    @Override
    public DataOperationInfo getCanUseSendData(String dataTableName, String relationTableName, List<String> sourceCodes, String missionByCode, String affiliateCode, String advertiserCode) {
        List<DatasourceInfo> datasourceInfos = datasourceMapper.getDataSourceByCodes(sourceCodes);
        DataOperationInfo dataOperationInfo =null;
        dataOperationInfo = dataOperationInfoMapper.getCanUseSendData(
                dataTableName,
                relationTableName,
                datasourceInfos,
                missionByCode,
                affiliateCode,
                advertiserCode
                );

        return dataOperationInfo;
    }

    @Override
    public void insertDataToRelation(String indexTableCode, DataOperationInfo data, MissionInfo missionByCode, String dataType, SourceConfig sourceConfig) {
        //已使用的资料记录到关联表中
        if(DataFileTypeEnum.DATA.name().equals(dataType) ){
            String tableName = SystemFunctionUtil.getDistributeTableNameById(data.getId(), dataType,true);

            // 查询已使用资料在关联表中是否存在
            RelationMissionDataInfo relationMissionDataInfo =  dataOperationInfoMapper.getDataRelationByDuplicateConditionAndDataSourceAndDataId(
                    missionByCode.getCode(),
                    missionByCode.getAffiliateCode(),
                    missionByCode.getAdvertiserCode(),
                    data.getDataSourceCode(),
                    data.getCode(),
                    data.getId(),
                    tableName);

            // 如果关联表中资料已使用，做更新；否则就插入已使用资料到关系表中
            if(!ObjectUtils.isEmpty(relationMissionDataInfo) ){

                relationMissionDataInfo.setCode(indexTableCode);
                dataOperationInfoMapper.updateRelationCode(relationMissionDataInfo,tableName);
            }else {
                relationMissionDataInfo = new RelationMissionDataInfo(
                        indexTableCode,
                        missionByCode.getCode(),
                        data.getDataSourceCode(),
                        missionByCode.getAdvertiserCode(),
                        missionByCode.getAffiliateCode(),
                        data.getCode(),
                        data.getId()
                );
                dataOperationInfoMapper.insertDataToRelationInfo(relationMissionDataInfo,tableName);
            }


        }

    }

    @Override
    public void deleteRelationData(String historyCode, String dataId, String dataType) {
        if(DataFileTypeEnum.DATA.name().equals(dataType)){
            String tableName = SystemFunctionUtil.getDistributeTableNameById(dataId, dataType, true);
            dataOperationInfoMapper.deleteRelationData(historyCode,tableName);
        }

    }

    @Override
    public void updateRelationData(String historyCode, String dataId, String dataType) {
        if(DataFileTypeEnum.DATA.name().equals(dataType)){
            String tableName = SystemFunctionUtil.getDistributeTableNameById(dataId, dataType, true);
            dataOperationInfoMapper.updateRelationData(historyCode,tableName);
        }
    }
}
